﻿using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;

namespace TowerDefence
{
    public class UpdateTowerUpgradeUI : MonoBehaviour
    {
        #region 单例模式
        // 创建一个静态的 UpdateTowerUpgradeUI 实例，确保只有一个实例存在
        public static UpdateTowerUpgradeUI instance;

        // 在 Awake 方法中进行初始化
        public void Awake()
        {
            // 如果已经存在一个实例，则输出错误信息并返回
            if (instance != null)
            {
                Debug.LogError("存在多个 UpdateTowerUpgradeUI 实例");
                return;
            }

            // 将当前实例赋给 instance
            instance = this;
        }
        #endregion

        // 游戏管理器的引用
        GameManager gameManager;

        // 升级按钮、出售按钮、关闭按钮、交换按钮的引用
        public Button upgradeButton;
        public Button sellButon;
        public Button closeButton;
        public Button swapButton;

        [Header("升级塔信息")]
        // 塔的图标、名称、伤害、攻击速度、射程、描述、升级花费、售价、消息的引用
        public Image towerSprite;
        public TextMeshProUGUI nameText;
        public TextMeshProUGUI damageText;
        public TextMeshProUGUI fireRateText;
        public TextMeshProUGUI rangeText;
        public TextMeshProUGUI descriptionText;
        public TextMeshProUGUI upgradeCostText;
        public TextMeshProUGUI sellValueText;
        public TextMeshProUGUI messageText;
        [Space]
        [Header("当前塔信息")]
        // 当前塔信息窗口的引用，包括图标、名称、伤害、攻击速度、射程、描述
        public GameObject currentStatWindow;
        public Image currentTowerSprite;
        public TextMeshProUGUI currentNameText;
        public TextMeshProUGUI currentDamageText;
        public TextMeshProUGUI currentFireRateText;
        public TextMeshProUGUI currentRangeText;
        public TextMeshProUGUI currentDescriptionText;
        [Space]
        // 交换按钮的图标
        public Image swap;

        // 在启用时禁用当前塔信息窗口
        private void OnEnable()
        {
            currentStatWindow.SetActive(false);
        }

        // 在 Start 方法中进行初始化
        private void Start()
        {
            // 获取游戏管理器的引用
            gameManager = FindObjectOfType<GameManager>();

            // 禁用当前 GameObject
            gameObject.SetActive(false);
        }

        public void UpdateUI(TowerData upgradeData, TowerData currentData)
        {
            // 清空提示消息文本
            messageText.text = null;

            // 如果升级数据不为空
            if (upgradeData != null)
            {
                // 更新升级后塔的信息
                towerSprite.sprite = upgradeData.icon;
                nameText.text = upgradeData.towerName;
                damageText.text = "伤害: " + upgradeData.minDamage + " - " + upgradeData.maxDamage;
                fireRateText.text = "射速: " + upgradeData.fireRate;
                rangeText.text = "射程: " + upgradeData.range;
                descriptionText.text = upgradeData.description;
                upgradeCostText.text = upgradeData.cost.ToString();
                sellValueText.text = upgradeData.sellValue.ToString();
            }
            else
            {
                // 如果升级数据为空，显示升级未解锁的提示消息
                messageText.text = "塔的升级还没有解锁!请从主界面的升级商店购买升级塔";
            }

            // 更新当前塔的信息
            currentTowerSprite.sprite = currentData.icon;
            currentNameText.text = currentData.towerName;
            currentDamageText.text = "伤害: " + currentData.minDamage + " - " + currentData.maxDamage;
            currentFireRateText.text = "射速: " + currentData.fireRate;
            currentRangeText.text = "射程: " + currentData.range;
            currentDescriptionText.text = currentData.description;
        }

        public void NullifyUI(TowerData upgradeData, TowerData currentData)
        {
            // 将所有 UI 元素置空
            towerSprite.sprite = null;
            nameText.text = null;
            damageText.text = null;
            fireRateText.text = null;
            rangeText.text = null;
            descriptionText.text = null;
            upgradeCostText.text = null;
            sellValueText.text = null;

            currentTowerSprite.sprite = null;
            currentNameText.text = null;
            currentDamageText.text = null;
            currentFireRateText.text = null;
            currentRangeText.text = null;
            currentDescriptionText.text = null;
        }


        public void SwapButton()
        {
            // 切换显示当前塔的统计窗口的状态
            currentStatWindow.SetActive(!currentStatWindow.activeInHierarchy);

            // 根据当前统计窗口的状态，设置交换按钮的颜色
            if (currentStatWindow.activeInHierarchy)
                swap.color = Color.gray;
            else
                swap.color = Color.white;
        }

        public void UpgradeTowerButton01()
        {
            // 调用GameManager的升级塔按钮01的方法
            gameManager.UpgradeTowerButton01();
        }

        public void SellTowerButton()
        {
            // 调用GameManager的出售塔按钮的方法
            gameManager.SellTowerButton();
        }

    }
}
